package com.aisino.nmg.task;

import com.aisino.nmg.ant.env.Environment;
import com.aisino.nmg.ant.task.AbstractTask;
import com.aisino.nmg.ant.task.GeneratorToolTask;
import com.aisino.nmg.constant.GeneratorConstants;
import com.aisino.nmg.mybatis.generator.CustomMyBatisGenerator;
import com.aisino.nmg.template.EngineBuilder;
import com.aisino.nmg.template.TemplateEngine;
import com.aisino.nmg.util.FileUtil;
import com.aisino.nmg.util.PathUtil;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class InitProjectDBTask extends AbstractTask {
	private static final String GENERATOR_FILE = "generator-config.xml";

	public InitProjectDBTask(GeneratorToolTask parent) {
		super(parent);
	}

	@Override
	public String getName() {
		return "SSM 项目数据初始化";
	}

	@Override
	public Integer getSort() {
		return new Integer(2);
	}

	@Override
	public void execute() {
		Environment environment = this.parent.getEnvironment();
		// 生成generatorConfig.xml配置文件
		EngineBuilder engineBuilder = new EngineBuilder(PathUtil.getCurrentProjectPath() + GeneratorConstants.RESOURCES_SOURCE_PATH);
		TemplateEngine templateEngine = engineBuilder.getTemplateEngine("freemarker");
		FileUtil.createFilePath(PathUtil.getCurrentProjectPath(getClass()) + GeneratorConstants.RESOURCES_SOURCE_PATH, GENERATOR_FILE);
		Map<String, Object> model = new HashMap<>();
		model.put("driver", environment.getDbDriver());
		model.put("url", environment.getDbUrl());
		model.put("username", environment.getDbUsername());
		model.put("password", environment.getDbPassword());
		model.put("modelPackage", "暂无数据");
		model.put("daoPackage", "暂无数据");
		model.put("javaModelPath", "暂无数据");
		model.put("sqlMapPath", "暂无数据");
		model.put("javaClientPath", "暂无数据");
		Boolean createFileFlag = FileUtil.createFilePath(PathUtil.getCurrentProjectPath(
				getClass()) + GeneratorConstants.RESOURCES_SOURCE_PATH, GENERATOR_FILE);
		if(createFileFlag){
			templateEngine.process(model, "generator" + File.separator + "generatorConfig.ftl", FileUtil.writeFileToPath(
					PathUtil.getCurrentProjectPath(
							getClass()) + GeneratorConstants.RESOURCES_SOURCE_PATH, GENERATOR_FILE));
			InputStream is = null;
			try {
				is = new FileInputStream(PathUtil.getCurrentProjectPath(getClass()) + GeneratorConstants.RESOURCES_SOURCE_PATH + GENERATOR_FILE);
				ConfigurationParser cp = new ConfigurationParser(null);
				DefaultShellCallback shellCallback = new DefaultShellCallback(true);
				Configuration configuration = null;
				configuration = cp.parseConfiguration(is);
				CustomMyBatisGenerator customMyBatisGenerator = new CustomMyBatisGenerator(configuration, shellCallback, null);
				customMyBatisGenerator.generate(null);
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				if(is != null){
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
